## @author Dean
## @info  自动化构建部署
## 脚本前置：
## 1 当前文件有 ${service_name}.img 的镜像文件
## 2 当前文件有 docker-compose.yml 的镜像文件

usage(){
   echo "Usage: `basename $0` [-s <service_name[string]>]"
   exit -1
}

while getopts "s:" opts;
do
    case $opts in
        s) service_name=$OPTARG
        ;;
        *) usage
        ;;
    esac
done

if [ -z "${service_name}" ];
then
    usage
fi

source /etc/profile

command -v docker >/dev/null 2>&1 || { echo >&2 "please check the [docker] path or installed ?"; exit -1; }
command -v docker-compose >/dev/null 2>&1 || { echo >&2 "please check the [docker-compose] path or installed ?"; exit -1; }

docker load < ${service_name}.img

echo "==================================="

echo "===> stop container ${service_name} [start]"
docker-compose -f docker-compose.yml stop -t 10 ${service_name}
echo "===> stop container ${service_name} [over]"

echo "===> remove container ${service_name} [start]"
docker-compose -f docker-compose.yml rm -f ${service_name}
echo "===> remove container ${service_name} [over]"

echo "===> build and up service[${service_name}] container [start]"
docker-compose -f docker-compose.yml up -d ${service_name}
echo "===> build and up service[${service_name}] container [over]"

echo "===> prune none images [start]"
docker image prune -f
echo "===> prune none images [over]"

sleep 6s
docker-compose -f ${CURRENT_COMPOSE_PATH} ps